<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>确认订单</title>
    <meta name="keywords" content="">
    <meta name="description" content="">
    <meta name="renderer" content="webkit">
    <meta http-equiv="Cache-Control" content="no-siteapp" />
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black" />
    <meta name="format-detection" content="telephone=no" />
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, minimum-
scale=1, user-scalable=no">
    <link rel="stylesheet" type="text/css" href="../css/common.css" />
    <link rel="stylesheet" type="text/css" href="../css/y_index.css" />
    <style>

    </style>
</head>
<style type="text/css">
.wh {
    width: 0.68rem;
    height: 0.68rem;
    display: flex;
    color: #666;
    font-size: .32rem;
    justify-content: center;
    align-items: center;
}
</style>

<body>
    <div class="bigBox bw">
        <div class="container">
            <div class="addressBox">
                <div class="p20 flex_aj flex_sa bdb addresslist">
                    <img src="../img/y_map.png" style="width: .2rem;height: .26rem;">
                    <div class="flex flex_column">
                        请先添加默认地址
                    </div>
                    <img src="../img/y_arrow_right.png" style="width: .12rem;height: .22rem;">
                </div>
            </div>
            <div class="mainBox">
                <!--            <div class="flex p20"> <img src="../img/y_8.png" style="width:1.5rem;height:1.5rem;" /> <ul class="ml20"> <li>OBC 无线双耳超小隐形蓝牙挂耳式耳机运动跑 步必备</li> <li class="mt20 col9">分类：分类41</li> <li class="c4 mt20 flex_sb">￥1920.00 <div class="flex c_box"> <label class="wh minus border1 border-r1"> <button class="font18 col9">&minus;</button> </label> <input class="font18 wh tac num" type="number" name="" value="0" /> <label class="wh add bgce border-r1"> <button class="font18 col9">+</button> </label> </div> </li> </ul> </div> -->
            </div>
            <!-- <li class="flex_sb p20"> <span>运费</span> <span class="col9">￥10.00</span> </li> <li class="flex_a jce p20 bdb">共计1件商品 小计：<span class="c4">￥</span><span class="font19 c4">1920.00</span></li> -->
        </div>
        <div class="flex_sb" style="border-top: 1px solid #f5f5f5;">
            <span class="ml20">合计金额：<span class="c4 sum"><!-- ￥1920.00 --></span></span>
            <span class="b_diy1 p20 colw" onclick="pay()">提交订单</span>
        </div>
    </div>
</body>

</html>
<script src="../js/jquery-1.8.3.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../js/dlc.js" type="text/javascript" charset="utf-8"></script>
<script src="../js/swiper.min.js" type="text/javascript" charset="utf-8"></script>
<script src="https://gw.alipayobjects.com/as/g/h5-lib/alipayjsapi/3.1.0/alipayjsapi.inc.min.js"></script>
<!--公司的常用函数封装-->
<script type="text/javascript">
var total = getUrlParam('sum') * 1,
    status = 0,
    realMoney = 0, //实付金额
    sumTrack = 0, //总运费
    jsApiParameters = null, //支付初始化
    orderDetailList = [], //立即下单信息
    payArr = {},
    obj = {};

if (getLocalData('shopArr').length > 0) {
    var arr = getLocalData('shopArr');
    var str = '';
    arr.forEach((item, index) => {
        if (item.chose == 1) {
            str += '<div class="list" data-id=' + item.goodsId + ' data-name=' + item.goodsName + ' data-img=' + item.imgUrl + ' data-c_id=' + item.categoryId + ' data-price=' + item.goodsPrice + ' data-track=' + item.freight + '><div class="flex p20"> <img src="' + item.imgUrl + '" style="width:1.5rem;height:1.5rem;" /> <ul class="ml20 flex1"> <li>' + item.goodsName + '</li> <li class="mt20 col9">分类：' + item.c_name + '</li> <li class="c4 mt20 flex_sb">￥' + item.goodsPrice + '<div class="flex c_box"> <label class="wh minus border1 border-r1"> <button class="font18 col9" data-index="0" onclick="count(this)">&minus;</button> </label> <input class="font18 wh tac num" type="number" name="" value="' + item.goodsNum + '" disabled/> <label class="wh add bgce border-r1"> <button class="font18 col9" data-index="1" onclick="count(this)">+</button> </label> </div> </li> </ul> </div>   <li class="flex_sb p20"> <span>运费</span> <span class="col9">￥<span>' + item.freight + '</span> </li> <li class="flex_a jce p20 bdb">共计<span class="num1">' + item.goodsNum + '</span>件商品 小计：<span class="c4">￥</span><span class="font19 c4"><span class="total">' + Number(item.goodsNum * item.goodsPrice + item.freight).toFixed(2) + '</span></span></li></div>'
            sumTrack += item.freight; //总运费
            realMoney += item.goodsPrice * item.goodsNum;
        }
    })
    realMoney = sumTrack * 1 + realMoney; //实付金额
    $('.sum').text("￥" + realMoney.toFixed(2));
    if (str.length > 0) {
        $('.mainBox').html(str);
    } else {
        $('.mainBox').html(emptyTip('暂无数据'));
    }
} else {
    arr = [];
}

//加减计算
function count(_this) {
    var price = $(_this).parents('.list').data('price');
    var id = $(_this).parents('.list').data('id');
    var track = $(_this).parents('.list').data('track');
    if ($(_this).data('index') == 0) { //减
        var num = $(_this).parents('.list').find('.num').val() * 1 - 1;
        if (num >= 0) {
            total = total * 1 - price * 1;
            console.log(realMoney -= price * 1)
        }
    } else { //加
        var num = Number($(_this).parents('.list').find('.num').val()).toFixed(2) * 1 + 1;
        total = total * 1 + price * 1;
        realMoney = realMoney * 1 + price * 1;
    }
    if (num >= 0) {
        $(_this).parents('.list').find('.total').text(Number(price * num + track).toFixed(2));
        $(_this).parents('.list').find('.num').val(num);
        $(_this).parents('.list').find('.num1').text(num);
        $('.sum').text("￥" + Number(realMoney).toFixed(2));
        arr.forEach((item, index) => {
            if (item.goodsId == id) {
                item.goodsNum = $(_this).parents('.list').find('.num').val() * 1;
                status = 1;
            }
        })
        if (status) {
            saveLocalData('shopArr', arr);
        }
    }
}
// 获取地址信息
if (getLocalData('address').length > 0) {
    var str = '';
    var address = getLocalData('address');
    address.forEach((item, index) => {
        str += '<div class="p20 flex_aj flex_sa bdb addresslist"  data-id=' + item.addressId + '> <img src="../img/y_map.png" style="width: .2rem;height: .26rem;" /> <div class="flex flex_column flex1 plr40"> <li class="flex_sb"> 收货人：' + item.name + ' <span>' + item.phone + '</span> </li> <li class="mt20 col9">' + item.province + item.addr + '</li> </div> <img src="../img/y_arrow_right.png" style="width: .12rem;height: .22rem;" /> </div>'
        if (str.length > 0) {
            $('.addressBox').html(str);
        } else {
            $('.addressBox').html('<div class="p20 flex_aj flex_sa bdb addresslist"> <img src="../img/y_map.png" style="width: .2rem;height: .26rem;"> <div class="flex flex_column"> 请先添加默认地址 </div> <img src="../img/y_arrow_right.png" style="width: .12rem;height: .22rem;"> </div>')
        }
    })
}
//修改默认地址
$('.addresslist').click(function() {
    location.href = 'http://gxzjj.j.xiaozhuschool.com/h5/gxzjj/fd/html/zy_address.html?type=1';
})
//提交订单
function pay() {
    if ($('.addresslist').data('id') == undefined) {
        dlctipbox.show('请先添加默认地址');
    } else {
        var countNum = 0;
        for (var i = 0; i < $('.list').length; i++) {
            obj = {};
            countNum += Number($('.list').eq(i).find('.num').val()) * 1;
            obj.goodsId = $('.list').eq(i).data('id');
            obj.goodsName = $('.list').eq(i).data('name');
            obj.imgUrl = $('.list').eq(i).data('img');
            obj.goodsPrice = $('.list').eq(i).data('price') * 100;
            obj.categoryId = $('.list').eq(i).data('c_id');
            obj.goodsNum = Number($('.list').eq(i).find('.num').val()) * 1;
            realMoney += $('.list').eq(i).find('.total').text();
            orderDetailList.push(obj);
        }
        payArr.addrId = getLocalData('address')[0].addressId;
        payArr.goodsSum = total * 100; //商品金額
        payArr.producer = arr[0].producer;
        payArr.openId = localStorage.getItem('token');
        payArr.realPayment = $('.sum').text().split('￥')[1] * 100; //實付金額加上運費的
        payArr.orderDetailList = orderDetailList;
        payArr.orderNum=total
        console.log(orderDetailList);
        console.log(JSON.stringify(payArr));
        if (countNum == 0) {
            dlctipbox.show('请先添加商品')
        } else {
            dlc_request('/api/myWallet/balancePayment', { 'jsonArray': JSON.stringify(payArr) }, function(res) {
                console.log(res);
                if (res.code == 1) {
                    dlctipbox.show(res.msg);
                    // alert(JSON.stringify(res.data.packageParams))
                    if (res.data.packageParams) {
                        jsApiParameters = res.data.packageParams;
                        callpay();
                    } else {
                        dlctipbox.show('支付成功');
                        location.href = './ser_orderManage.html';
                    }
                    clearLocalData('shopArr');
                } else {
                    dlctipbox.show(res.msg);
                }
            })
        }
    }
}

function jsApiCall() {
    WeixinJSBridge.invoke(
        'getBrandWCPayRequest',
        jsApiParameters,
        function(res) {
            if (res.err_msg == 'get_brand_wcpay_request:ok') {
                location.href = './ser_orderManage.html';
            } else {
                onOff = 0;
                dlctipbox.show('支付取消或失败');
            }
        }
    );
}

function callpay() {
    if (typeof WeixinJSBridge == "undefined") {
        if (document.addEventListener) {
            document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
        } else if (document.attachEvent) {
            document.attachEvent('WeixinJSBridgeReady', jsApiCall);
            document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
        }
    } else {
        jsApiCall();
    }
}
</script>